package com.xiaoshuidi.cloud.module.contract.service.home;


import com.xiaoshuidi.cloud.module.contract.controller.admin.contract.vo.HomeQueryVO;
import com.xiaoshuidi.cloud.module.contract.controller.admin.home.vo.HomeData;
import com.xiaoshuidi.cloud.module.contract.dal.mysql.bill.RentOrderMapper;
import com.xiaoshuidi.cloud.module.contract.enums.contract.ContractHomeTypeEnum;
import com.xiaoshuidi.cloud.module.contract.service.home.dto.HomeDataDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.temporal.TemporalAdjusters;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class HomeServiceImpl implements HomeService {

    @Resource
    private RentOrderMapper rentOrderMapper;

    @Override
    public HomeData getHomeData(HomeQueryVO req) {
        LocalDate receivableDate = req.getStartDate();
        LocalDate endDate = receivableDate.plusDays(5);
        //查询
        List<HomeDataDTO> homeDataDTOList = rentOrderMapper.getHomeData(receivableDate, endDate,req.getApartmentId());
        HomeData homeData = new HomeData();
        Integer overdueTotal = 0;
        Integer todayTotal = 0;
        Integer fiveDaysTotal = 0;
        if (CollectionUtils.isNotEmpty(homeDataDTOList)) {
            for (HomeDataDTO homeDataDTO : homeDataDTOList) {
                overdueTotal += homeDataDTO.getExpired();
                todayTotal += homeDataDTO.getToday();
                fiveDaysTotal += homeDataDTO.getFiveDay();
                switch (ContractHomeTypeEnum.getByCode(homeDataDTO.getName())) {
                    case RENTORDER:
                        // 应收账单
                        homeData.setReceivableTotal(homeDataDTO.getReceivables());
                        break;
                    case OWNERRENTORDER:
                        // 应付账单
                        homeData.setMeetTotal(homeDataDTO.getReceivables());
                        break;
                    case CONTRACT:
                        // 租客合同
                        homeData.setContractTotal(homeDataDTO.getReceivables());
                        break;
                    case OWNERCONTRACT:
                        // 业主合同
                        homeData.setOwnerTotal(homeDataDTO.getReceivables());
                        break;
                    default:
                        break;
                }
            }
        }
        List<Map<LocalDate, Boolean>> homeMonthData = getHomeMonthData(req);
        homeData.setOverdueTotal(overdueTotal);
        homeData.setTodayTotal(todayTotal);
        homeData.setFiveDaysTotal(fiveDaysTotal);
        homeData.setMonthlyCalendar(homeMonthData);

        return homeData;
    }

    /**
     * 工作台 月待办
     */
    @Override
    public List<Map<LocalDate, Boolean>> getHomeMonthData(HomeQueryVO req) {
        LocalDate receivableDate = req.getStartDate();
        receivableDate = receivableDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endDate = receivableDate.with(TemporalAdjusters.lastDayOfMonth());
        //查询
        List<HomeDataDTO> homeDataDTOList = rentOrderMapper.getHomeMonthData(receivableDate, endDate,req.getApartmentId());
        List<Map<LocalDate, Boolean>> mapList = getList(req.getStartDate(), homeDataDTOList);
        return mapList;
    }


    @Override
    public List<Map<LocalDate, Boolean>> getHomeMonthBill(HomeQueryVO req) {
        LocalDate receivableDate=req.getStartDate();
        receivableDate = receivableDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endDate = receivableDate.with(TemporalAdjusters.lastDayOfMonth());
        //查询
        List<HomeDataDTO> homeDataDTOList = rentOrderMapper.getHomeMonthBill(receivableDate, endDate,req.getApartmentId());
        List<Map<LocalDate, Boolean>> mapList = getList(receivableDate, homeDataDTOList);
        return mapList;
    }

    @Override
    public List<Map<LocalDate, Boolean>> getHomeMonthContract(HomeQueryVO req) {
        LocalDate receivableDate=req.getStartDate();
        receivableDate = receivableDate.with(TemporalAdjusters.firstDayOfMonth());
        LocalDate endDate = receivableDate.with(TemporalAdjusters.lastDayOfMonth());
        //查询
        List<HomeDataDTO> homeDataDTOList = rentOrderMapper.getHomeMonthContract(receivableDate, endDate,req.getApartmentId());
        List<Map<LocalDate, Boolean>> mapList = getList(receivableDate, homeDataDTOList);

        return mapList;
    }

    @NotNull
    private static List<Map<LocalDate, Boolean>> getList(LocalDate startDate, List<HomeDataDTO> homeDataDTOList) {
        //生成当月的每一天
        List<LocalDate> list = monthlyCalendar(startDate);
        List<HomeDataDTO> collect = homeDataDTOList.stream().distinct().collect(Collectors.toList());
        List<Map<LocalDate, Boolean>> mapList = new ArrayList<>();
        for (LocalDate localDate : list) {
            Map<LocalDate, Boolean> map = new HashMap<>();
            for (HomeDataDTO homeDataDTO : collect) {
                if (homeDataDTO.getDayTime().compareTo(localDate)==0) {
                    map.put(localDate, Boolean.TRUE);
                    break;
                }
            }
            if (map.isEmpty()) {
                map.put(localDate, Boolean.FALSE);
            }
            mapList.add(map);
        }
        return mapList;
    }

    public static List<LocalDate> monthlyCalendar(LocalDate date) {
        //以构造该对象时的日期构造一个新对象
        int month = date.getMonthValue(); //得到该日期的月
        int today = date.getDayOfMonth(); //获得该日期的日
        date = date.minusDays(today - 1); //将日期设置为该月的第一天

        List<LocalDate> list = new ArrayList<>();
        while (date.getMonthValue() == month) {
            list.add(date);
            date = date.plusDays(1);
        }
        return list;
    }
}
